package com.young.mylib.view.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


import androidx.recyclerview.widget.RecyclerView;

import com.young.mylib.R;

import java.util.List;

/**
 * Author：Lucry on 2017/2/4 10:43
 */
public class SimpleListPopupViewAdapter extends RecyclerView.Adapter<SimpleListPopupViewAdapter.CacheListViewHolder> {
    private List<String> carList = null;
    private String id = "";
    private onItemSelectedListener mOnItemSelectedListener;

    class CacheListViewHolder extends RecyclerView.ViewHolder {
        TextView tvId;
        public CacheListViewHolder(View itemView) {
            super(itemView);
            tvId = (TextView) itemView.findViewById(R.id.tv_id);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if(mOnItemSelectedListener!=null){

                        mOnItemSelectedListener.onItemSelected(tvId.getText().toString());
                        notifyDataSetChanged();
                    }
                }
            });
        }
    }

    public SimpleListPopupViewAdapter(List<String> appInfos) {
        this.carList = appInfos;
    }

    public interface onItemSelectedListener {
        void onItemSelected(String select);
    }


    public void updateData(List<String> appInfos) {
        this.carList = appInfos;
        id = "";
        notifyDataSetChanged();
    }

    @Override
    public CacheListViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_popup_view_item, null);
        return new CacheListViewHolder(view);
    }

    @Override
    public void onBindViewHolder(final CacheListViewHolder holder, int position) {
        final String str = carList.get(position);
        holder.tvId.setText(str);
    }

    @Override
    public int getItemCount() {
        return carList == null ? 0 : carList.size();
    }


    public void setOnItemSelectedListener(onItemSelectedListener onItemSelectedListener) {
        mOnItemSelectedListener = onItemSelectedListener;
    }
}
